package com.hongxun.net;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.hongxun.budget.greendao.DPictureEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.greendao.ECrossCostEntityDao;
import com.hongxun.budget.greendao.EDeviceBuyEntityDao;
import com.hongxun.budget.greendao.EDeviceOldEntityDao;
import com.hongxun.budget.greendao.EDrawLineEntityDao;
import com.hongxun.budget.greendao.EMaterialBuyEntityDao;
import com.hongxun.budget.greendao.EMaterialOldEntityDao;
import com.hongxun.budget.greendao.EPRangeEntityDao;
import com.hongxun.budget.greendao.EPWireEntityDao;
import com.hongxun.budget.greendao.EPointEntityDao;
import com.hongxun.budget.greendao.EProblemexistEntityDao;
import com.hongxun.budget.greendao.EWorkContentEntityDao;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.greenentity.tk.DPictureEntity;
import com.hongxun.greenentity.ys.ECrossCostEntity;
import com.hongxun.greenentity.ys.EDeviceBuyEntity;
import com.hongxun.greenentity.ys.EDeviceOldEntity;
import com.hongxun.greenentity.ys.EDrawLineEntity;
import com.hongxun.greenentity.ys.EMaterialBuyEntity;
import com.hongxun.greenentity.ys.EMaterialOldEntity;
import com.hongxun.greenentity.ys.EPBaseEntity;
import com.hongxun.greenentity.ys.EPRangeEntity;
import com.hongxun.greenentity.ys.EPWireEntity;
import com.hongxun.greenentity.ys.EPointEntity;
import com.hongxun.greenentity.ys.EProblemexistEntity;
import com.hongxun.greenentity.ys.EWorkContentEntity;
import com.hongxun.interfaces.NovaCallback;

import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/9/18.
 */

public class CheckNet extends BaseNet {

    /**
     * 上传图片
     *
     * @param context
     * @param uuid
     * @param c
     */
    public static void upPhoto(Context context, String uuid, NovaCallback c) {
        List<DPictureEntity> list = GreenDaoManager.getInstance().getmDaoSession().getDPictureEntityDao().queryBuilder().where(
                DPictureEntityDao.Properties.PGuid.eq(uuid), DPictureEntityDao.Properties.PDatastate.notEq("删除")
                , DPictureEntityDao.Properties.PStatus.eq("验收")
                , DPictureEntityDao.Properties.PDatastate.notEq("已同步")
        ).list();
//        ZipUtils.zip(list);//先打包文件
//        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data; charset=utf-8"), new File(ZipUtils.zipFilePath));
//        RequestBody requestBody = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM)
//                .addFormDataPart("fileName", "zip", requestFile)
//                .addFormDataPart("photo", JSON.toJSONString(list))
//                .build();
//        getNovate(context, "图片信息上传中...").rxUploadWithBody("", Contants.photoUpload, requestBody, new Callback(2, c, d));
        getDialog(context,"图片信息上传中");
        upPic(context,list,c,2);
    }

    //项目列表
    public static void projectInfo(Context context, NovaCallback c) {
        getNovate(context, "获取项目列表").rxPost(Contants.problemInfo, getParams(context), new Callback(999, c, d));
    }

    //下载项目
    public static void downProject(Context context, String uuid, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        map.put("uuid", uuid);
        getNovate(context, "正在下载项目").rxPost(Contants.downProblem, map, new Callback(888, c, d));
    }

    //更新项目信息(基本信息)
    public static void updateProject(Context context, String uuid, NovaCallback c) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        Map<String, Object> map = getParams(context);
        EPBaseEntity epBaseEntity = session.getEPBaseEntityDao().load(uuid);
        List<ECrossCostEntity> eCrossCostEntities = session.getECrossCostEntityDao().queryBuilder().where(
                ECrossCostEntityDao.Properties.PGuid.eq(uuid)
                , ECrossCostEntityDao.Properties.CCDatastate.notEq("已同步")
        ).list();
        List<EDrawLineEntity> eDrawLineEntities = session.getEDrawLineEntityDao().queryBuilder().where(
                EDrawLineEntityDao.Properties.PGuid.eq(uuid)
                , EDrawLineEntityDao.Properties.DLDatastate.notEq("已同步")
        ).list();
        List<EPointEntity> ePointEntities = session.getEPointEntityDao().queryBuilder().where(
                EPointEntityDao.Properties.PGuid.eq(uuid)
                , EPointEntityDao.Properties.PPDatastate.notEq("已同步")
        ).list();
        List<EPRangeEntity> epRangeEntities = session.getEPRangeEntityDao().queryBuilder().where(
                EPRangeEntityDao.Properties.PGuid.eq(uuid)
                , EPRangeEntityDao.Properties.PRDatastate.notEq("已同步")
        ).list();
        List<EPWireEntity> epWireEntities = session.getEPWireEntityDao().queryBuilder().where(
                EPWireEntityDao.Properties.PGuid.eq(uuid)
                , EPWireEntityDao.Properties.PWDatastate.notEq("已同步")
        ).list();
        List<EProblemexistEntity> eProblemexistEntities = session.getEProblemexistEntityDao().queryBuilder().where(
                EProblemexistEntityDao.Properties.Projectguid.eq(uuid)
                , EProblemexistEntityDao.Properties.Datastate.notEq("已同步")
        ).list();
        List<EDeviceOldEntity> eDeviceOldEntities = session.getEDeviceOldEntityDao().queryBuilder().where(
                EDeviceOldEntityDao.Properties.PGuid.eq(uuid)
                , EDeviceOldEntityDao.Properties.DRDatastate.notEq("已同步")
        ).list();
        List<EDeviceBuyEntity> eDeviceBuyEntities = session.getEDeviceBuyEntityDao().queryBuilder().where(
                EDeviceBuyEntityDao.Properties.PGuid.eq(uuid)
                , EDeviceBuyEntityDao.Properties.DBDatastate.notEq("已同步")
        ).list();
        List<EMaterialOldEntity> eMaterialOldEntities = session.getEMaterialOldEntityDao().queryBuilder().where(
                EMaterialOldEntityDao.Properties.PGuid.eq(uuid)
                , EMaterialOldEntityDao.Properties.MBDatastate.notEq("已同步")
        ).list();
        List<EMaterialBuyEntity> eMaterialBuyEntities = session.getEMaterialBuyEntityDao().queryBuilder().where(
                EMaterialBuyEntityDao.Properties.PGuid.eq(uuid)
                , EMaterialBuyEntityDao.Properties.MBDatastate.notEq("已同步")
        ).list();
        List<EWorkContentEntity> eWorkContentEntities = session.getEWorkContentEntityDao().queryBuilder().where(
                EWorkContentEntityDao.Properties.PGuid.eq(uuid)
                , EWorkContentEntityDao.Properties.WCDatastate.notEq("已同步")
        ).list();
        map.put("epBaseEntity", JSON.toJSONString(epBaseEntity));
        map.put("eCrossCostEntities", JSON.toJSONString(eCrossCostEntities));
        map.put("eDrawLineEntities", JSON.toJSONString(eDrawLineEntities));
        map.put("ePointEntities", JSON.toJSONString(ePointEntities));
        map.put("epRangeEntities", JSON.toJSONString(epRangeEntities));
        map.put("epWireEntities", JSON.toJSONString(epWireEntities));
        map.put("eProblemexistEntities", JSON.toJSONString(eProblemexistEntities));
        map.put("eDeviceOldEntities", JSON.toJSONString(eDeviceOldEntities));
        map.put("eDeviceBuyEntities", JSON.toJSONString(eDeviceBuyEntities));
        map.put("eMaterialOldEntities", JSON.toJSONString(eMaterialOldEntities));
        map.put("eMaterialBuyEntities", JSON.toJSONString(eMaterialBuyEntities));
        map.put("eWorkContentEntities", JSON.toJSONString(eWorkContentEntities));
        getNovate(context, "项目基础信息上传中...").rxPost(Contants.problemUpdate, map, new Callback(3, c, d));
    }

}
